/***************************************************************************
 *   Copyright (C) 2006 by latarro   *
 *   glatarow@bastardo   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "gjpeg2khandler.h"
#include <QImageIOHandler>


// ---------------------------------------------------------------------------
/*!
 * Image I/O plugin for a JPEG 2000 compressed file format.
 *
 * It uses OpenJPEG library for image reading/writing.
 *
 * No writing support yet.
 * TODO: writing support.
 */
// ---------------------------------------------------------------------------
class GJpeg2kPlugin : public QImageIOPlugin
{
public:
        /*!
        * Construct the plugin.
        */
    GJpeg2kPlugin();
        /*!
        * Plugin destructor.
        */
    ~GJpeg2kPlugin();

        /*!
        * Returns a list of the format string recognized by the plugin.
        * Currently return only "jp2" string.
        */
    QStringList keys() const;
        /*!
        * Determines the read/write capabilities of the plugin
        *
        * or rather, of the handler it provides, based on a given
        * I/O device or format string. When a format string is not given,
        * the plugin calls the static function canRead() of GJpeg2kHandler
        * to determine if the device's contents indicate the presence of
        * a JPEG 2000 image.
        *
        * @param device device on witch plugin determines read/write capabilities
        * @param format format string on witch plugin can determine
        *
        * @return read/write capabilities
        */
    Capabilities capabilities(QIODevice *device,
                              const QByteArray &format) const;
        /*!
        * Creates an instance of the image I/O handler itself.
        *
        * @param device the device to handle with
        * @param format a specific file format
        *
        * @return JPEG 2000 image I/O handler
        */
    QImageIOHandler *create(QIODevice *device,
                            const QByteArray &format = QByteArray()) const;
    
}; // class GJpeg2kPlugin{}
// ---------------------------------------------------------------------------


// ---------------------------- GJpeg2kPlugin() ------------------------------
GJpeg2kPlugin::GJpeg2kPlugin()
{
}
// ---------------------------------------------------------------------------


// ---------------------------- ~GJpeg2kPlugin() -----------------------------
GJpeg2kPlugin::~GJpeg2kPlugin()
{
}
// ---------------------------------------------------------------------------


// --------------------------- keys() ----------------------------------------
QStringList GJpeg2kPlugin::keys() const
{
        // return supported format strings 
    return QStringList() << "jp2";
}
// ---------------------------------------------------------------------------


// ----------------------------- capabilities() ------------------------------
QImageIOPlugin::Capabilities GJpeg2kPlugin::capabilities(QIODevice *device,
        const QByteArray &format) const
{
        // Determine read/write capabilities based on format string 
    if(format == "jp2") {
        // correct file format

            // return read && write capabilities 
        return Capabilities(CanRead/* | CanWrite*/);
    }
    if(!(format.isEmpty() && device->isOpen())) {
        // incorrect file format

            // return capabilities that says: no read and write
        return 0;
    }

        // format string is empty, determine read/write capabilities
        // based on device state 
    Capabilities cap;
    if(device->isReadable() && GJpeg2kHandler::canRead(device)) {
        // device is readable and the correct magic was found

            // extend capabilities for reading 
        cap |= CanRead;
    }
// TODO uncomment this when writing will be available
//     if(device->isWritable()) {
//         // device is writable
// 
//             // extend capabilities for writing 
//         cap |= CanWrite;
//     }

        // return capabilities determined on device state 
    return cap;
}
// ---------------------------------------------------------------------------


// --------------------------- create() --------------------------------------
QImageIOHandler *GJpeg2kPlugin::create(QIODevice *device,
                                       const QByteArray &format) const
{
        // create JPEG 2000 handler 
    QImageIOHandler *handler = new GJpeg2kHandler;
        // set its properties 
    handler->setDevice(device);
    handler->setFormat(format);
        // return created handler
    return handler;
}
// ---------------------------------------------------------------------------


// ---------------------------------------------------------------------------
// export plugin macros for Qt detection
Q_EXPORT_STATIC_PLUGIN(GJpeg2kPlugin);
Q_EXPORT_PLUGIN2(gjpeg2k, GJpeg2kPlugin);



